package com.sky.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.controller.admin.DishController;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    /**
     * 新增菜品
     * @param dishDTO                               ※ 偏复杂
     */
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        //把属性放入类内
        BeanUtils.copyProperties(dishDTO,dish);
        //添加菜品属性
        dishMapper.addDish(dish);

        //添加口味属性
        //获取自增字段的值
        //在插入SQL时写入了useGeneratedKeys="true" keyProperty="id" 会返回自增id的值
        Long id = dish.getId();

        //获取口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                //设置菜品ID
                //* 应该是把这个口味的id设置为菜品id 标志这个口味是这个菜品的
                dishFlavor.setDishId(id);
            });
            dishFlavorsMapper.insertFlavors(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> page=dishMapper.dishPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品起售，停售开关
     * @param status
     * @param id
     */
    @Override
    public void updatedish(Integer status, Long id) {
        Dish dish=Dish.builder()
                .status(status)
                .id(id)
                .build();

        dishMapper.update(dish);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO dishById(Long id) {
        //根据菜品id查询菜品数据
        Dish dish=dishMapper.getById(id);

        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors=dishFlavorsMapper.getFlavors(id);

        //两种数据查询到后 封装到VO内
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    @Transactional//开启事务 确保是原子操作
    public void modifDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        //拷贝赋值到新类
        BeanUtils.copyProperties(dishDTO,dish);

        //修改菜品基本信息
        dishMapper.update(dish);

        //删除原有的口味数据
        dishFlavorsMapper.deleteDishFlavors(dishDTO.getId());

        //插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                //设置口味的菜品ID
                dishFlavor.setDishId(dishDTO.getId());
            });
            //插入口味数据
            dishFlavorsMapper.insertFlavors(flavors);
        }

    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional //事务注解 保证原子操作
    public void deleteDish(List<Long> ids) {

        for (Long id : ids) {
            //查询各个菜品
            Dish dish=dishMapper.getById(id);
            //判断状态码是否纬停售
            if(dish.getStatus()!= StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否被套餐关联 查询两个表 id是否在套餐的表内 套餐表为 setmeal
        List<Long> setmealIds=setmealMapper.DgetSetmealIdByDishId(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品中菜品的数据
        dishMapper.deleteDish(ids);
        //删除菜品关联的口味数据dish_flavor
        dishFlavorsMapper.deleteDish(ids);

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getFlavors(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 根据分类id 查询所有菜品
     * @param id
     * @return
     */
    public List<DishVO> ListByIdDish(Long id) {
        return dishMapper.ListByIDDish(id);
    }

}
